/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package model.dao;


import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import model.dao.services.NewsCategoryDAOservice;
import model.enetities.NewsCategory;

/**
 *
 * @author Ku81
 */


public class NewsCategoryDAO implements NewsCategoryDAOservice{
    
    
    private static NewsCategoryDAO newsCategoryDAO;

    public static NewsCategoryDAO getInstance() {
        if (newsCategoryDAO == null) {
            newsCategoryDAO = new NewsCategoryDAO();
        }
        return newsCategoryDAO;
    }

    @Override
    public List<NewsCategory> getNewsCategory(boolean isActived) {
        List<NewsCategory> listCategory = new ArrayList<NewsCategory>();
        try {
            Connection conn = ConnectionFactory.getConnection();
            String sql = "select * from tblNewsCategory where isActived = ?";
            PreparedStatement pstmt = conn.prepareStatement(sql);

            ResultSet rs = pstmt.executeQuery();
            while (rs.next()) {
                NewsCategory nc = new NewsCategory();
                nc.setIdCategory(rs.getInt("idCategory"));
                nc.setName(rs.getString("name"));
                nc.setDescriptions(rs.getString("descriptions"));
                listCategory.add(nc);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return listCategory;
    }

    @Override
    public NewsCategory getNewsCategoryByID(int idCategory,boolean isActived) {
        NewsCategory nc = new NewsCategory();
        try {
            Connection con = ConnectionFactory.getConnection();
            String sql = "select * from tblNewsCategory where idCategory = ? and isActived = ?";
            PreparedStatement pstmt = con.prepareStatement(sql);
            pstmt.setInt(1, idCategory);
            ResultSet rs = pstmt.executeQuery();
            while (rs.next()) {
                nc.setIdCategory(rs.getInt("idCategory"));
                nc.setName(rs.getString("name"));
                nc.setDescriptions(rs.getString("descriptions"));
;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return nc;
    }

    @Override
    public boolean createrNewsCategory(NewsCategory category) {
        boolean isCheck = false;
        try {
 
            Connection conn = ConnectionFactory.getConnection();
            String sql = "insert into tblNewsCategory values (?,?,?)";
            PreparedStatement pstmt = conn.prepareStatement(sql);
            pstmt.setString(1, category.getName());
            pstmt.setString(2, category.getDescriptions());
            pstmt.setBoolean(3, category.isActived());
            
            pstmt.executeUpdate();
            isCheck = true;
        } catch (SQLException ex) {
            Logger.getLogger(NewsDAO.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(NewsDAO.class.getName()).log(Level.SEVERE, null, ex);
        }
        return isCheck;
    }

    @Override
    public boolean updateNewsCategory(NewsCategory category) {
        boolean isCheck = false;
        try {
            //throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
            Connection con = ConnectionFactory.getConnection();
            String sql = "update tblNewsCategory set name = ?, descriptions = ?, isActived = ? where idCategory = ? ";
            PreparedStatement pstmt = con.prepareStatement(sql);
            pstmt.setString(1, category.getName());
            pstmt.setString(2, category.getDescriptions());
            pstmt.setBoolean(3, category.isActived());
            pstmt.setInt(4, category.getIdCategory());
            
            pstmt.executeUpdate();
            isCheck = true;
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(NewsDAO.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(NewsDAO.class.getName()).log(Level.SEVERE, null, ex);
        }
        return isCheck;
        
    }

    @Override
    public boolean deleteNewsCategory(int idCategory) {
        boolean isCheck = false;
        try {
            //throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
            Connection con = ConnectionFactory.getConnection();
            String sql = "update tblNewsCategory set isActived ='false' where idCategory = ?";
            PreparedStatement pstmt  =  con.prepareStatement(sql);
            pstmt.setInt(1, idCategory);
            
            pstmt.executeUpdate();
            isCheck = true;
        } catch (SQLException ex) {
            Logger.getLogger(NewsDAO.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(NewsDAO.class.getName()).log(Level.SEVERE, null, ex);
        }
        return isCheck;
    }

    
    
}
